﻿@model Bricks.Site.Models.ContractBillInfoView
@{
    ViewBag.Title = "ContractBillInfoList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        var contractID = $("#hContractID").val();
        initTable(contractID);
    });
    var rowIndex = -1;
    function initTable(contractID) {
        $('#myDataGrid').datagrid({
            url: '/ContractBillInfo/GetContractBillInfoList?pid=' + contractID,//一个用以从远程站点请求数据的超链接地址
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            pagination: true,
            pageSize: 20,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            fitColumns: true,
            columns: [[
                        { field: 'ck', checkbox: true },   //选择
                        { title: 'ID', field: 'ID', hidden: true },
                        { title: '清单编号', field: 'BillCode', width: 80, sortable: true },
                        { title: '清单名称', field: 'BillName', width: 150, sortable: true },
                        { title: '清单类型', field: 'BillType', width: 70, sortable: true },
                        { title: '原单价', field: 'Price', width: 50, sortable: true },
                        { title: '现单价', field: 'CurrentPrice', width: 50, sortable: true, editor: { type: 'numberbox', options: { precision: 2 } } },
                        { title: '数量', field: 'Count', width: 70, sortable: true },
                        { title: '现数量', field: 'CurrentCount', width: 70, sortable: true, editor: 'numberbox' },
                        { title: '金额', field: 'Money', width: 80, sortable: true },
                        { title: '现金额', field: 'CurrentMoney', width: 80, sortable: true },
                        {
                            title: '操作', field: 'Action', width: 70, align: 'center',
                            formatter: function (value, row, index) {
                                if (row.editing) {
                                    var s = '<a href="#" onclick="saverow(' + index + ')">保存</a> ';
                                    var c = '<a href="#" onclick="cancelrow(' + index + ')">取消</a>';
                                    return s + c;
                                } else {
                                    return '<a href="#" onclick="editrow(' + index + ')">编辑</a> ';
                                }
                            }
                        }
            ]],
            onBeforeEdit: function (index, row) {
                row.editing = true;
                $('#myDataGrid').datagrid('refreshRow', index);
            },
            onAfterEdit: function (index, row) {
                row.editing = false;
                row.CurrentMoney = row.CurrentPrice * row.CurrentCount;
                $('#myDataGrid').datagrid('refreshRow', index);
            },
            onCancelEdit: function (index, row) {
                row.editing = false;
                $('#myDataGrid').datagrid('refreshRow', index);
            },
            onDblClickRow: function (index, row) {
                $('#myDataGrid').datagrid('beginEdit', index);
                rowIndex = index;
            },
            toolbar: [{
                id: 'btnConfirm',
                text: '确认',
                iconCls: 'icon-save',
                handler: function () {

                }
            }, '-', {
                id: 'btnCancel',
                text: '取消',
                iconCls: 'icon-close_square',
                handler: function () {
                    window.parent.closePop();
                }
            }]
        });
    }

    function editrow(index) {
        $('#myDataGrid').datagrid('beginEdit', index);
    }

    function saverow(index){ 
        $('#myDataGrid').datagrid('endEdit', index);
    } 

    function cancelrow(index) {
        $('#myDataGrid').datagrid('cancelEdit', index);
    }
</script>
<div class="easyui-panel" @Html.Raw(ViewBag.PanelInfo)>
    <input type="hidden" id="hContractID" value="@Model.PID" />
    <table id="myDataGrid" fit="true"></table>
</div>
